package ru.arriah.servicedesk.web.dispatcher.action;

import java.io.IOException;
import java.util.Collection;

import javax.naming.NamingException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import ru.arriah.common.web.action.ActionMapping;
import ru.arriah.common.web.action.DumbAction;
import ru.arriah.servicedesk.bean.ClientBean;
import ru.arriah.servicedesk.bean.ClientTypeBean;
import ru.arriah.servicedesk.bean.DepartmentBean;
import ru.arriah.servicedesk.bean.OrganizationBean;
import ru.arriah.servicedesk.ejb.ClientManagerLocalHome;
import ru.arriah.servicedesk.ejb.ClientManagerLocalObject;
import ru.arriah.servicedesk.ejb.OrganizationManagerLocalHome;
import ru.arriah.servicedesk.ejb.OrganizationManagerLocalObject;
import ru.arriah.servicedesk.help.Utils;
import ru.arriah.servicedesk.web.commonaction.exception.InternalException;


public class ClientFormAction extends DumbAction {
   protected ClientManagerLocalHome clientManagerHome;
   protected OrganizationManagerLocalHome organizationManagerHome;    
   
   public ClientFormAction() throws NamingException {
      super();
      clientManagerHome =
         Utils.getClientManagerHomeInterface();      
      organizationManagerHome =
        Utils.getOrganizationManagerHomeInterface();
   }

   public String execute(
      ActionMapping actionMapping,
      HttpServletRequest request,
      HttpServletResponse response
   ) throws IOException, ServletException {
      try {        
        ClientManagerLocalObject clientManager = 
          clientManagerHome.create();
        OrganizationManagerLocalObject organizationManager =
          organizationManagerHome.create();
        
        Collection<ClientTypeBean> clientTypesList = 
          clientManager.selectClientTypes();         
        
        Collection<OrganizationBean> organizationsList = 
          organizationManager.selectOrganizations();
        
        /* TODO: select departments only for ARRIAH - remove stub */
        Collection<DepartmentBean> departmentsList = 
           organizationManager.selectDepartments(1);
         
         request.setAttribute("departmentsList", departmentsList);
        
        int clientId = idVal(request.getParameter("client_id"));        
        
        if(clientId != 0) {          
          ClientBean clientBean = clientManager.selectClient(clientId);
          clientBean.setRePassword(clientBean.getPassword());
          request.setAttribute("clientBean", clientBean);                                     
        }                       
        
        clientManager.remove();
        organizationManager.remove();                
        
        request.setAttribute("organizationsList", organizationsList);
        request.setAttribute("clientTypesList", clientTypesList);        
        
        return actionMapping.getSuccessTarget();
      } catch (Exception e) {
         handleException(e, "execute");
         throw new InternalException("ErrorMessage.internal");
      }
   }
}